package com.leetcode.dataStruct.tree;

import com.leetcode.dataStruct.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class FlattenToSingleList {
    public static void main(String[] args) {

    }

    public void flatten(TreeNode root) {
        List<TreeNode> preNodeList = new ArrayList<>();
        preorder(root, preNodeList);
        for (int i = 1; i < preNodeList.size(); i++) {
            TreeNode pre = preNodeList.get(i - 1);
            TreeNode cur = preNodeList.get(i);
            pre.left = null;
            pre.right = cur;
        }
    }

    private void preorder(TreeNode root, List<TreeNode> nodeList) {
        if (root != null) {
            nodeList.add(root);
            preorder(root.left, nodeList);
            preorder(root.right, nodeList);
        }
    }
}
